import TreeCesiumData from "./layer_tree_data";
import Environment from "@/plugins/utils/environment";
import LayerManager from "@/plugins/mev/layer/LayerManager";
import { color } from "echarts/src/export";
import { Alert } from "element-ui";

class GeoJsonData extends TreeCesiumData {
    constructor(data, viewer) {
        super(data, viewer);
        this._DataObject = null;
        /*      this._geoJsonDataSource = null;*/
        this._data = data;
        //geojson生成的对象
        this._geojson = null;
        this._viewer = viewer;
        this._vectorType = "";
    }

    show() {
        let that = this;
        /* that._geojson = Environment.getDataBasePath() + that._data.path;*/
        that._geojson = that._data.path;

        const str = that._geojson.split("/");
        const image_path = str[0] + "/" + str[1] + "/" + str[2] + "/" + str[3] + "/icon";
        const image_number = String(str[6]).substr(0, 2);

        // Cesium.Resource.fetchJson(that._geojson).then(jsonData => {
        //     that._geojson = jsonData;
        // });
        let promise = Cesium.GeoJsonDataSource.load(that._geojson, {});
        promise.then(function(dataSource) {
            that._DataObject = dataSource;
            that._viewer.dataSources.add(dataSource);
            let entities = dataSource.entities.values;
            for (let i = 0; i < entities.length; i++) {
                let entity = entities[i];

                if (entity.polyline) {
                    that._vectorType = LayerManager.GEOJSON_POLYLINE;
                    entity.polyline.material = new Cesium.Color.fromCssColorString(that._data.lineColor.toString());
                    entity.polyline.width = that._data.width;
                    entity.polyline.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(that._data.minshow, that._data.maxshow);
                } else if (entity.polygon) {
                    that._vectorType = LayerManager.GEOJSON_POLYGON;
                    entity.polygon.outline = true;
                    entity.polygon.outlineWidth = that._data.width.toString();
                    entity.polygon.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(that._data.minshow, that._data.maxshow);
                    //多边形填充颜色
                    entity.polygon.material = new Cesium.Color.fromCssColorString(that._data.fillColor.toString());
                    //线颜色
                    entity.polygon.outlineColor = new Cesium.Color.fromCssColorString(that._data.lineColor.toString());
                } else {
                    let name = String(entities[i].properties.NAME);
                    that._vectorType = LayerManager.POINT;
                    entity.billboard = {
                        image: image_path + "/" + image_number + ".png",
                        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                        scale: 0.7,
                        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(that._data.minshow, that._data.maxshow),
                        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                        disableDepthTestDistance: 0
                    };
                    entity.label = {
                        //文字标签
                        text: name,
                        scale: 0.3,
                        //scaleByDistance: new Cesium.NearFarScalar(300000, 0.5, 1000000, 0.2),
                        font: "48px Helvetica",
                        fillColor: new Cesium.Color.fromCssColorString(that._data.fillColor.toString()),
                        outlineColor: Cesium.Color.BLACK,
                        outlineWidth: 3,
                        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                        verticalOrigin: Cesium.VerticalOrigin.TOP,
                        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(that._data.minshow, that._data.maxshow / 3),
                        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                        disableDepthTestDistance: 0
                        //中心位置
                        // pixelOffset: new Cesium.Cartesian2(0, 0)
                    };
                }
            }

            /*        that.flyTo(that._DataObject);*/
        });
    }

    close() {
        let that = this;
        if (this._DataObject) {
            if (this._viewer.dataSources.contains(this._DataObject)) {
                this._viewer.dataSources.remove(this._DataObject, true);
            }
            this._DataObject = null;
        }

        that.handler.removeInputAction(Cesium.ScreenSpaceEventType.PINCH_END);
    }
}

export default GeoJsonData;
